<?php  if ( ! defined('APPPATH')) exit('No direct script access allowed');
/**
 *      [PinTuXiu] (C)2001-2099 ONightjar.com Pintuxiu.com.
 *      This is NOT a freeware, use is subject to license terms
*/
class topic_controller extends PZ_Controller{

	public function __construct() {
		parent::__construct();
		$this->authfunctions = array('addlike','removelike','delete');
	}

	public function listing(){
		$num_per_page = 10;
		$ptx_topic = load_db("topic");
		$category_id =  $this->param("cid");
		$sort =  $this->param("sort");
		$args = array();
		if($category_id){
			$this->category_id = $conditions['category_id'] = $args['cid'] = $category_id;
		}
		
		$this->load_seo('topic');

		if($sort){
			$this->sort = $args['sort'] = $sort;
		}
		
		if($sort=='dtime'){
			$sort = " ptx_topic.create_time DESC ";
		}elseif($sort=='atime'){
			$sort = " ptx_topic.create_time ASC ";
		}elseif($sort=='dlike'){
			$sort = " ptx_topic.total_like DESC ";
		}
		$this->page = $this->param('page','1');
		$topics = $ptx_topic->search($conditions,$this->page,$num_per_page,null,$sort);
		$pager = &load_class('Pager', 'core');
		$this->pages = $pager->createPages('topic/listing',$args);
		//$shareapi = load_api('share');
		/* foreach ($topics as $key=>$topic){
			$share_conditions['keyword'] = 'topic'.pzxencode($topic['topic_title']).'t';
			$share_conditions['type'] = 'noarticle';
			$shares = $shareapi->search($share_conditions,1,9,null);
			$topics[$key]['shares'] = $shareapi->prepareShares($shares);
			$pageData = $pager->getPager();
			$topics[$key]['numbershares'] = $pageData['total_count'];
			$topics[$key]['keywords'] = ($topic['keyword'])?encodekeywordsarray(explode(',', $topic['keyword'])):null;
		} */
		$this->topics = load_api('topic')->prepareTopics($topics);
		$this->newest10 = $ptx_topic->search_no_page(null,null,null,10);
		$this->output("topic/listing");
	}

	public function show(){
		$this->lastest();
	}
	
	public function hot(){
		//$this->seo_title($this->topic['topic_title'].' '.T('hot_pin'));
		$this->output_pin('hot', " ptx_share.total_like DESC ");
	}

	public function lastest(){
		//$this->seo_title($this->topic['topic_title'].' '.T('lastest_pin'));
		$this->output_pin('lastest', " ptx_share.create_time DESC ");
	}

	public function goods(){
		//$this->seo_title($this->topic['topic_title'].' '.T('goods'));
		$conditions['type'] = 'goods';
		$this->output_pin('goods', " ptx_share.create_time DESC ",$conditions);
	}

	public function video(){
		//$this->seo_title($this->topic['topic_title'].' '.T('video'));
		$conditions['type'] = 'video';
		$this->output_pin('video', " ptx_share.create_time DESC ",$conditions);
	}

	public function share(){
		//$this->seo_title($this->topic['topic_title'].' '.T('share'));
		$conditions['type'] = 'share';
		$this->output_pin('share', " ptx_share.create_time DESC ",$conditions);
	}
	
	public function article(){
		//$this->seo_title($this->topic['topic_title'].' '.T('article'));
		$conditions['type'] = 'article';
		$this->output_pin('article', " ptx_share.create_time DESC ",$conditions);
	}
	
	private function output_pin($action,$order,$conditions=array()){
		if($topicid =  $this->param("topicid")){
			$topicdb = load_db("topic");
			$this->topic = $topicdb->find_one(array('topic_id'=>$topicid));
			$topicdb->add_view($topicid);
		}
		
		if(!$this->topic){
			$this->redirect(site_url('topic/listing'), lang('404_error_content','tip'));
			return;
		}

		$api = load_api('setting');
		$setting = $api->getSetting('layoutpin');
		$pagesize = $setting['uipin_numperpage']?$setting['uipin_numperpage']:15;
		$numbershowpage = $setting['uipin_numbershowpage']?$setting['uipin_numbershowpage']:0;
		$page = $this->param('page','1');
		$wpage = $this->param('wpage');
		$args = array("page"=>"2","wf"=>"1","topicid"=>$this->topic['topic_id']);
		if($wpage&&$numbershowpage){
			$data['page'] = ($wpage-1)*$numbershowpage+$page;
			$args['wpage'] = $data['wpage'] = $wpage;
		}else{
			$data['page'] = $page;
		}
		$data['showpage'] = (($numbershowpage)&&($data['page']%$numbershowpage==0))?'1':'0';

		if($data['page']==1){
			$this->liked = load_db("topic/favtopic")->find_one(array('user_id'=>$this->cuser['uid'],'topic_id'=>$this->topic['topic_id']));
			$this->seo_title($this->topic['topic_title']);
			$this->seo_keyword($this->topic['keyword'].' '.$this->topic['topic_title']);
			$this->seo_description($this->topic['topic_desc'].' '.$this->topic['topic_title']);
		}
		$conditions['keyword'] =  'topic'.pzxencode($this->topic['topic_title']).'t';
	
		$data['nextpage_url'] = site_url_param('topic/'.$action,$args);
		$shareapi = load_api('share');
		$shares = $shareapi->search($conditions,$data['page'],$pagesize,$order);
		$shares = $shareapi->prepareShares($shares);
		if($setting['uipin_ads']){
			$shares = $shareapi->add_ads($shares);
		}
		$pager = &load_class('Pager', 'core');
		unset($args['wf']);
		$data['pages'] = $pager->createPages('topic/'.$action,$args);
		$pageData = $pager->getPager();
		$data['numbershares'] = $pageData['total_count'];
		unset($args['page']);
		$data['wpages'] = $pager->createCustomPages('topic/'.$action,$args,'wpage',$pagesize*$numbershowpage);
		$data['tpl_waterfall'] = $this->render('waterfall/pin',array('shares'=>$shares,'showpage'=>$data['showpage']));
		$data['pinsetting'] = $setting;
		$this->output("topic/show",$data,($data['page']==1)?true:false);
	}
	
	public function addlike(){
		if($topicid =  $this->param("topicid")){
			$this->topic = load_db("topic")->find_one(array('topic_id'=>$topicid));
		}
		
		if(!$this->topic){
			$this->redirect(site_url('topic/listing'), lang('404_error_content','tip'));
			return;
		}
		if($this->topic){
			$data['user_id'] = $this->cuser['uid'];
			$data['topic_id'] = $this->topic['topic_id'];
			$result = load_db("topic/favtopic")->add_one($data);
			if($result){
				load_db("topic")->add_like($this->topic['topic_id']);
				$this->ajax_success_response(null, 'success');
			}else{
				$this->ajax_failed_response('like_already');
			}
		}
	
	}
	
	public function removelike(){
		if($topicid =  $this->param("topicid")){
			$this->topic = load_db("topic")->find_one(array('topic_id'=>$topicid));
		}
	
		if(!$this->topic){
			$this->redirect(site_url('topic/listing'), lang('404_error_content','tip'));
			return;
		}
		if($this->topic){
			$data['user_id'] = $this->cuser['uid'];
			$data['topic_id'] = $this->topic['topic_id'];
			if(load_db("topic/favtopic")->find_one($data)){
				$result = load_db("topic/favtopic")->delete($data);
				load_db("topic")->remove_like($this->topic['topic_id']);
				$this->ajax_success_response(null, 'success');
			}else{
				$this->ajax_failed_response('like_already');
			}
		}
	
	}
	
	public function delete(){
		if($topicid =  $this->param("topicid")){
			$this->topic = load_db("topic")->find_one(array('topic_id'=>$topicid));
		}
		
		if(!$this->topic){
			$this->redirect(site_url('topic/listing'), lang('404_error_content','tip'));
			return;
		}
		
		if($this->isadmin||$this->iseditor||$this->topic['user_id']==$this->cuser['uid']){
			load_api('topic')->delete($topicid);
			$this->redirect(site_url('home/topics'), lang('200_deletesuccess','tip'));
		}
	}

}